<?php

namespace App\Http\Controllers\Api;

use App\Areas;
use App\FakeRank;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class AreasController extends Controller
{
    public function rank(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => [
                'required',
                Rule::in([0,1,2,3,4,5,6])
            ]
        ]);
        if ($validator->fails()) {
            return ['error' => '参数错误'];
        }
        $data = [];
        $areas = Areas::all();
        if ($request->type == 0) {
            foreach ($areas as $area) {
                $data[$area->name] = $area->getMyAreaCount();
            };
        } else {
            foreach ($areas as $area) {
                $data[$area->name] = $area->getMyGoodsCount($request->type);
            }
        }
        return $data;
    }

    public function fakeRank(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => [
                'required',
                Rule::in([0,1,2,3,4,5,6])
            ]
        ]);
        if ($validator->fails()) {
            return ['error' => '参数错误'];
        }
        $areas = FakeRank::where('type', $request->type)->first();
        $areas_data = [
            '东北' => 0,
            '西北' => 0,
            '京津' => 0,
            '晋冀蒙' => 0,
            '山东' => 0,
            '河南' => 0,
            '两湖' => 0,
            '苏皖' => 0,
            '浙沪' => 0,
            '西南' => 0,
            '云贵广' => 0,
            '粤海' => 0,
            '闽赣' => 0,
        ];
        if ($request->type != 0) {
            $areas_data = [
                '东北' => $areas->dongbei,
                '西北' => $areas->xibei,
                '京津' => $areas->jingjin,
                '晋冀蒙' => $areas->jinjimeng,
                '山东' => $areas->shandong,
                '河南' => $areas->henan,
                '两湖' => $areas->lianghu,
                '苏皖' => $areas->suwan,
                '浙沪' => $areas->zhehu,
                '西南' => $areas->xinan,
                '云贵广' => $areas->yunguiguang,
                '粤海' => $areas->yuehai,
                '闽赣' => $areas->mingan,
            ];
            $orders = $areas->getGoodsCount();
            $money = $orders * $GLOBALS['goods'][$request->type - 1]->price;
        } else {
                $data = $this->getGoodsData();
                $areas_data = $data['data'];
                $orders = $data['orders'];
                $money = $this->getMoneyCount();
        }

        return ['data' => $areas_data, 'money' => $money, 'orders' => $orders];
    }

    protected function getGoodsData()
    {
        $fakers = FakeRank::all();
        $rows = [];
        $data = [
            '东北' => 0,
            '西北' => 0,
            '京津' => 0,
            '晋冀蒙' => 0,
            '山东' => 0,
            '河南' => 0,
            '两湖' => 0,
            '苏皖' => 0,
            '浙沪' => 0,
            '西南' => 0,
            '云贵广' => 0,
            '粤海' => 0,
            '闽赣' => 0,
        ];
        foreach ($fakers as $faker) {
            $data['东北'] += $faker->dongbei;
            $data['西北'] += $faker->xibei;
            $data['京津'] += $faker->jingjin;
            $data['晋冀蒙'] += $faker->jinjimeng;
            $data['山东'] += $faker->shandong;
            $data['河南'] += $faker->henan;
            $data['两湖'] += $faker->lianghu;
            $data['苏皖'] += $faker->suwan;
            $data['浙沪'] += $faker->zhehu;
            $data['西南'] += $faker->xinan;
            $data['云贵广'] += $faker->yunguiguang;
            $data['粤海'] += $faker->yuehai;
            $data['闽赣'] += $faker->mingan;
        }
        foreach ($data as $datum){
            array_push($rows, $datum);
        }
        return ['data' => $data,'orders' =>array_sum($rows)];
    }

    protected function getMoneyCount()
    {
        $fakers = FakeRank::all();
        $rows = [];
        $data = [
            'dongbei' => 0,
            'xibei' => 0,
            'jingjin' => 0,
            'jinjimeng' => 0,
            'shandong' => 0,
            'henan' => 0,
            'lianghu' => 0,
            'suwan' => 0,
            'zhehu' => 0,
            'xinan' => 0,
            'yunguiguang' => 0,
            'yuehai' => 0,
            'mingan' => 0,
        ];
        foreach ($fakers as $key => $faker) {
            $data['dongbei'] += $faker->dongbei * $GLOBALS['goods'][$key]->price;
            $data['xibei'] += $faker->xibei * $GLOBALS['goods'][$key]->price;
            $data['jingjin'] += $faker->jingjin * $GLOBALS['goods'][$key]->price;
            $data['jinjimeng'] += $faker->jinjimeng * $GLOBALS['goods'][$key]->price;
            $data['shandong'] += $faker->shandong * $GLOBALS['goods'][$key]->price;
            $data['henan'] += $faker->henan * $GLOBALS['goods'][$key]->price;
            $data['lianghu'] += $faker->lianghu * $GLOBALS['goods'][$key]->price;
            $data['suwan'] += $faker->suwan * $GLOBALS['goods'][$key]->price;
            $data['zhehu'] += $faker->zhehu * $GLOBALS['goods'][$key]->price;
            $data['xinan'] += $faker->xinan * $GLOBALS['goods'][$key]->price;
            $data['yunguiguang'] += $faker->yunguiguang * $GLOBALS['goods'][$key]->price;
            $data['yuehai'] += $faker->yuehai * $GLOBALS['goods'][$key]->price;
            $data['mingan'] += $faker->mingan * $GLOBALS['goods'][$key]->price;
        }
        foreach ($data as $datum){
            array_push($rows, $datum);
        }
        return array_sum($rows);
    }
}
